<?php
namespace App\Service;

use Illuminate\Support\Facades\DB;

class BaseService
{
    //基础服务

    protected $database = 'mysql'; //库名称
    protected $db; //表对象

    protected $dbUser = 'user'; //用户
    protected $dbUlog = 'user_login'; //登录LOG
    protected $dbMkConfig = 'mk_config'; //生成器配置
    protected $dbMkUpload = 'mk_upload'; //用户设计图
    protected $dbMkWord = 'mk_word'; //功能库
    protected $dbMkUser = 'mk_user'; //用户功能库

    //返回参数
    public function toback($code = 0, $msg = '', $data = [], $total = 0)
    {
        $msg = $msg ? $msg : 'success';
        return array('code' => $code, 'msg' => $msg, 'data' => $data, 'total' => $total);
    }

    /**
     * 连接数据库
     * @param string $db 表名
     * @param array $ws 查询条件【key=>val】
     */
    public function dbConn($db, $ws=[])
    {
        $db = DB::connection($this->database)->table($db);
        if (!empty($ws)) {
            foreach ($ws as $k => $v) {
                $db = $db->where($k, $v);
            }
        }
        return $db;
    }
}